{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "75417ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "72f50802",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>fare_amount</th>\n",
       "      <th>extra</th>\n",
       "      <th>mta_tax</th>\n",
       "      <th>tip_amount</th>\n",
       "      <th>tolls_amount</th>\n",
       "      <th>improvement_surcharge</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>congestion_surcharge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-01 00:46:40</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1.65</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>9.95</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-01 00:59:47</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.6</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>16.30</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-21 13:48:30</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>5.80</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-11-28 15:52:25</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>7.55</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-11-28 15:56:57</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>55.55</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime  passenger_count  trip_distance  fare_amount  extra  \\\n",
       "0  2019-01-01 00:46:40              1.0            1.5          7.0    0.5   \n",
       "1  2019-01-01 00:59:47              1.0            2.6         14.0    0.5   \n",
       "2  2018-12-21 13:48:30              3.0            0.0          4.5    0.5   \n",
       "3  2018-11-28 15:52:25              5.0            0.0          3.5    0.5   \n",
       "4  2018-11-28 15:56:57              5.0            0.0         52.0    0.0   \n",
       "\n",
       "   mta_tax  tip_amount  tolls_amount  improvement_surcharge  total_amount  \\\n",
       "0      0.5        1.65           0.0                    0.3          9.95   \n",
       "1      0.5        1.00           0.0                    0.3         16.30   \n",
       "2      0.5        0.00           0.0                    0.3          5.80   \n",
       "3      0.5        0.00           0.0                    0.3          7.55   \n",
       "4      0.5        0.00           0.0                    0.3         55.55   \n",
       "\n",
       "   congestion_surcharge  \n",
       "0                   NaN  \n",
       "1                   NaN  \n",
       "2                   NaN  \n",
       "3                   NaN  \n",
       "4                   NaN  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Import the taxi info from both January and July 2019.\n",
    "# Include the following columns: tpep_pickup_datetime, passenger_count, trip_distance,\n",
    "# fare_amount, extra, mta_tax, tip_amount, tolls_amount, improvement_surcharge,\n",
    "# total_amount, and congestion_surcharge.\n",
    "\n",
    "filenames = ['../data/nyc_taxi_2019-01.csv', '../data/nyc_taxi_2019-07.csv']\n",
    "\n",
    "all_dfs = [pd.read_csv(one_filename, \n",
    "           usecols=['tpep_pickup_datetime', 'passenger_count', 'trip_distance',\n",
    "                    'fare_amount','extra','mta_tax','tip_amount','tolls_amount',\n",
    "                    'improvement_surcharge','total_amount','congestion_surcharge'],\n",
    "           parse_dates=['tpep_pickup_datetime'])\n",
    "           for one_filename in filenames]\n",
    "\n",
    "df = pd.concat(all_dfs)\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e50fa600",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a new column, pre_tip_amount\n",
    "df['pre_tip_amount'] = (\n",
    "    df\n",
    "    [['fare_amount', 'extra', 'mta_tax', 'tolls_amount', \n",
    "      'improvement_surcharge', 'congestion_surcharge']]\n",
    "    .sum(axis='columns')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4e8a58e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a new column, tip_percentage, showing the percent of the fare_amount that people tipped\n",
    "df['tip_percentage'] = df['tip_amount'] / df['pre_tip_amount']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "da53a378",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.13003974566357937"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What was the overall tip percentage?\n",
    "df['tip_percentage'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c3e69f9d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tip_percentage\n",
       "False    13970379\n",
       "True         7832\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# How many times did people tip more than 100%?\n",
    "(df['tip_percentage'] > 1).value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "98552a52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tip_percentage\n",
       "False    0.67923\n",
       "True     0.32077\n",
       "Name: proportion, dtype: float64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What percent of taxi riders give no tip at all?\n",
    "(df['tip_percentage'] == 0).value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "352a2c0b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "3    0.133970\n",
       "2    0.132221\n",
       "1    0.131424\n",
       "4    0.129136\n",
       "0    0.128723\n",
       "6    0.126634\n",
       "5    0.125801\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# On which day of the week do people tip the greatest percentage?\n",
    "# Mon = 0, Tues = 1, Wed = 2, Thu = 3, Fri = 4, Sat = 5, Sun = 6\n",
    "\n",
    "df.groupby(df['tpep_pickup_datetime'].dt.day_of_week)['tip_percentage'].mean().sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3ee84377",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "22    0.138816\n",
       "20    0.138160\n",
       "21    0.137685\n",
       "8     0.137116\n",
       "19    0.135174\n",
       "23    0.134978\n",
       "18    0.133292\n",
       "9     0.133017\n",
       "7     0.132134\n",
       "0     0.131490\n",
       "2     0.130914\n",
       "1     0.130710\n",
       "17    0.128640\n",
       "10    0.127200\n",
       "11    0.125022\n",
       "16    0.124655\n",
       "13    0.124567\n",
       "12    0.124376\n",
       "14    0.123727\n",
       "15    0.123547\n",
       "3     0.121053\n",
       "6     0.119915\n",
       "4     0.118987\n",
       "5     0.112028\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# At which hour do people tip the greatest percentage?\n",
    "df.groupby(df['tpep_pickup_datetime'].dt.hour)['tip_percentage'].mean().sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4396ea84",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "5     0.200000\n",
       "8     0.158099\n",
       "3     0.148046\n",
       "9     0.141431\n",
       "1     0.137011\n",
       "2     0.132224\n",
       "7     0.121570\n",
       "12    0.109367\n",
       "6     0.107354\n",
       "10    0.100000\n",
       "4     0.074877\n",
       "11    0.046026\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Do people tip more, on average, in January or July?\n",
    "df.groupby(df['tpep_pickup_datetime'].dt.month)['tip_percentage'].mean().sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5f65e5ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "2019-02-13    0.358127\n",
       "2019-02-25    0.250000\n",
       "2019-08-20    0.241865\n",
       "2019-11-27    0.200000\n",
       "2019-08-15    0.200000\n",
       "2019-05-20    0.200000\n",
       "2019-08-10    0.200000\n",
       "2019-09-22    0.200000\n",
       "2019-09-24    0.200000\n",
       "2019-09-25    0.200000\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# What was the 1-day period in our data set when people tipped the greatest percentage?\n",
    "df = df.set_index('tpep_pickup_datetime')\n",
    "df.resample('1D')['tip_percentage'].mean().sort_values(ascending=False).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "db8540a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "2019-01-31    0.144351\n",
       "2019-01-30    0.143530\n",
       "2019-01-24    0.143434\n",
       "2019-01-22    0.142769\n",
       "2019-01-15    0.142329\n",
       "2019-01-29    0.141330\n",
       "2019-01-10    0.141291\n",
       "2019-01-16    0.141147\n",
       "2019-01-17    0.140356\n",
       "2019-01-23    0.140309\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Try again, within our range\n",
    "df = df.sort_index()\n",
    "\n",
    "df = pd.concat([df.loc['2019-01-01':'2019-01-31'],\n",
    "           df.loc['2019-07-01':'2019-07-31']])\n",
    "\n",
    "df.resample('1D')['tip_percentage'].mean().sort_values(ascending=False).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5388c8d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "2019-01-31    0.137012\n",
       "2019-07-31    0.121570\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('1M')['tip_percentage'].mean().dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c95720f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
